Python/Ruby

推荐列表 站点导航

当前位置:首页 > 脚本编程 > Python/Ruby >

Python3.4123062015年3月验证码识别

来源:网络整理  作者:网友投稿  发布时间:2020-12-27 12:47
importsslimportjsonfromPILimportImageimporturllibimportreimporturllib requestasurllib2ifhasattr(ssl, 39;_create_unverified_context 39;):ssl create_defa...
import ssl import json from PIL import Image import urllib import re import urllib.request as urllib2 if hasattr(ssl, '_create_unverified_context'): ssl.create_default_context = ssl._create_unverified_context UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36" pic_url = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand&0.21191171556711197" def get_img(): resp = urllib2.urlopen(pic_url) raw = resp.read() with open('./tmp.jpg', 'wb') as fp: fp.write(raw) return Image.open('./tmp.jpg') def get_sub_img(im, x, y): assert 0 <= x <= 3 assert 0 <= y <= 2 WITH = HEIGHT = 68 left = 5 + (67 + 5) * x top = 41 + (67 + 5) * y right = left + 67 bottom = top + 67 return im.crop((left, top, right, bottom)) def baidu_stu_lookup(im): url = "?fr=html5&needRawImageUrl=true&id=WU_FILE_0&name=233.png&type=image%2Fpng&lastModifiedDate=Mon+Mar+16+2015+20%3A49%3A11+GMT%2B0800+(CST)&size=" im.save("./query_temp_img.png") raw = open("./query_temp_img.png", 'rb').read() url = url + str(len(raw)) req = urllib2.Request(url, raw, {'Content-Type': 'image/png', 'User-Agent': UA}) resp_url = urllib2.urlopen(req).read() url = "?queryImageUrl=" + urllib2.quote(resp_url) req = urllib2.Request(url, headers={'User-Agent': UA}) resp = urllib2.urlopen(req) html = resp.read().decode() return baidu_stu_html_extract(html) def baidu_stu_html_extract(html): pattern = re.compile(r"keywords:'(.*?)'") matches = pattern.findall(html) if not matches: return '[UNKOWN]' json_str = matches[0] json_str = json_str.replace('\\x22', '"').replace('\\\\', '\\') result = [item['keyword'] for item in json.loads(json_str)] return '|'.join(result) if result else '[UNKOWN]' if __name__ == '__main__': im = get_img() for y in range(2): for x in range(4): im2 = get_sub_img(im, x, y) result = baidu_stu_lookup(im2) print((y, x), result)

改自https://github.com/andelf/fuck12306/blob/master/fuck12306.py

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/python/9661.shtml

最新文章
python日常一 利用python抓取 python日常一 利用python抓取

时间:2021-01-17

一个 ARP 请求分组询问协议 一个 ARP 请求分组询问协议

时间:2021-01-17

此时就需要web抓取 此时就需要web抓取

时间:2021-01-17

这节我们使用Bootstrap 这节我们使用Bootstrap

时间:2021-01-17

我们该如何运用Python 来统 我们该如何运用Python 来统

时间:2020-12-27

python生成汉字图片字库 python生成汉字图片字库

时间:2020-12-26

python通过protobuf实现rpc python通过protobuf实现rpc

时间:2020-12-26

djngo快速实现使用Bootstra djngo快速实现使用Bootstra

时间:2020-12-26

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

Python3.4123062015年3月验证码识别

2020-12-27 编辑:网友投稿

import ssl import json from PIL import Image import urllib import re import urllib.request as urllib2 if hasattr(ssl, '_create_unverified_context'): ssl.create_default_context = ssl._create_unverified_context UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36" pic_url = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand&0.21191171556711197" def get_img(): resp = urllib2.urlopen(pic_url) raw = resp.read() with open('./tmp.jpg', 'wb') as fp: fp.write(raw) return Image.open('./tmp.jpg') def get_sub_img(im, x, y): assert 0 <= x <= 3 assert 0 <= y <= 2 WITH = HEIGHT = 68 left = 5 + (67 + 5) * x top = 41 + (67 + 5) * y right = left + 67 bottom = top + 67 return im.crop((left, top, right, bottom)) def baidu_stu_lookup(im): url = "?fr=html5&needRawImageUrl=true&id=WU_FILE_0&name=233.png&type=image%2Fpng&lastModifiedDate=Mon+Mar+16+2015+20%3A49%3A11+GMT%2B0800+(CST)&size=" im.save("./query_temp_img.png") raw = open("./query_temp_img.png", 'rb').read() url = url + str(len(raw)) req = urllib2.Request(url, raw, {'Content-Type': 'image/png', 'User-Agent': UA}) resp_url = urllib2.urlopen(req).read() url = "?queryImageUrl=" + urllib2.quote(resp_url) req = urllib2.Request(url, headers={'User-Agent': UA}) resp = urllib2.urlopen(req) html = resp.read().decode() return baidu_stu_html_extract(html) def baidu_stu_html_extract(html): pattern = re.compile(r"keywords:'(.*?)'") matches = pattern.findall(html) if not matches: return '[UNKOWN]' json_str = matches[0] json_str = json_str.replace('\\x22', '"').replace('\\\\', '\\') result = [item['keyword'] for item in json.loads(json_str)] return '|'.join(result) if result else '[UNKOWN]' if __name__ == '__main__': im = get_img() for y in range(2): for x in range(4): im2 = get_sub_img(im, x, y) result = baidu_stu_lookup(im2) print((y, x), result)

改自https://github.com/andelf/fuck12306/blob/master/fuck12306.py

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/python/9661.shtml

相关文章

风云图片

推荐阅读

返回Python/Ruby频道首页